package com.longma.server.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longma.server.entity.AlarmSetting;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.dto.AlarmSettingAddDTO;
import com.longma.server.pojo.dto.AlarmSettingUpdateDTO;
import com.longma.server.pojo.dto.PageRequestDTO;
import com.longma.server.service.IAlarmSettingService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@RestController
@Validated
@RequestMapping("/api/alarm")
public class AlarmSettingController {

    @Autowired
    private IAlarmSettingService alarmService;

    @ApiOperation(value = "新增告警设置")
    @PostMapping("/add")
    public UserResponseEntity<AlarmSetting> addAlarmSetting(@Valid @RequestBody AlarmSettingAddDTO addRequestDTO){
        return UserResponseEntity.success(alarmService.addAlarmSetting(addRequestDTO));
    }

    @ApiOperation(value = "根据告警id删除告警设置")
    @PostMapping("/del")
    public UserResponseEntity<Boolean> deleteAlarmSetting( @NotNull(message = "告警设置id不能为空") @RequestParam(name = "id") Integer id){
        return UserResponseEntity.success(alarmService.deleteAlarmSetting(id));
    }

    @ApiOperation(value = "根据告警id列表删除告警设置")
    @PostMapping("/dellist")
    public UserResponseEntity<Boolean> deleteAlarmSettingByIds(@RequestParam(name="ids") @Size(min = 1,message = "告警设置id列表不能为空")
                                                               List<@Valid @NotNull(message = "告警设置id不能为空") Integer> ids){
        return UserResponseEntity.success(alarmService.deleteAlarmSettingByIds(ids));
    }

    @ApiOperation(value = "更新告警设置")
    @PostMapping("/update")
    public UserResponseEntity<AlarmSetting> updateAlarmSetting( @Valid @RequestBody AlarmSettingUpdateDTO updateRequestDTO){
        return UserResponseEntity.success(alarmService.updateAlarmSetting(updateRequestDTO));
    }


    @ApiOperation(value = "获取所有报警配置(分页)")
    @PostMapping("/list")
    public UserResponseEntity<IPage<AlarmSetting>> getByPage(@ApiParam(value = "分页参数",required = true) @Valid @RequestBody PageRequestDTO pageRequestDTO){
        return UserResponseEntity.success(alarmService.getAlarmSettingByPage(pageRequestDTO.getPageNum(),
                pageRequestDTO.getPageSize(),pageRequestDTO.getAll(),pageRequestDTO.getConditions()));
    }



}

